/*
 * =====================================================================================
 *
 *       Filename:  junctions.cpp
 *
 *    Description:  Returns number of junctions
 *
 *        Version:  1.0
 *        Created:  12/07/12 15:35:17
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "thin.h"

using namespace std;
using namespace cv;

float junctions(Mat src) {
    resize(src, src, Size(0,0), 0.1, 0.1, INTER_NEAREST);
    Mat skel = thin(src);

    Size s = skel.size();

    Mat juncMat = Mat(s.height, s.width, CV_8UC1, Scalar(0));


    float junc = 0;

    // outer loop
    for (int i=1; i < s.height - 1; i++) {
        for (int j=1; j < s.width - 1; j++) {
            if (skel.at<unsigned char>(i, j) != 0) {
                // inner loop
                int on = -1;
                for (int k=-1; k <= 1; k++) {
                    for (int l=-1; l <= 1; l++) {
                        if (skel.at<unsigned char>(i + k, j + l) == 255)
                            on++;
                    }
                }
                if (on >= 3)
                    junc += 1;
            }
        }
    }

    return junc;

}
